const SearchAlgorithms = require("../template")

function testBinarySearchDetailed() {
  const searcher = new SearchAlgorithms()

  console.log(
    "1. 基本二分搜索测试:",
    (() => {
      const sortedArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
      const existingElements = [1, 7, 13, 25]
      return existingElements.every(target => {
        const result = searcher.binarySearch([...sortedArray], target)
        const expected = sortedArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "2. 不存在的元素测试:",
    (() => {
      const sortedArray = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]
      const nonExistingElements = [0, 2, 6, 12, 20, 26]
      return nonExistingElements.every(target => searcher.binarySearch([...sortedArray], target) === -1)
    })()
  )

  console.log(
    "3. 不同长度数组测试:",
    (() => {
      const arrays = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]
      return arrays.every(arr => {
        return arr.every((target, targetIndex) => {
          const result = searcher.binarySearch([...arr], target)
          return result === targetIndex
        })
      })
    })()
  )

  console.log(
    "4. 边界值测试:",
    (() => {
      const boundaryArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      return searcher.binarySearch([...boundaryArray], 1) === 0 &&
             searcher.binarySearch([...boundaryArray], 10) === 9 &&
             searcher.binarySearch([...boundaryArray], 0) === -1 &&
             searcher.binarySearch([...boundaryArray], 11) === -1
    })()
  )

  console.log(
    "5. 负数数组测试:",
    (() => {
      const negativeArray = [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
      return searcher.binarySearch([...negativeArray], -6) === 2 &&
             searcher.binarySearch([...negativeArray], 0) === 5 &&
             searcher.binarySearch([...negativeArray], 6) === 8 &&
             searcher.binarySearch([...negativeArray], -5) === -1
    })()
  )

  console.log(
    "6. 大数组测试:",
    (() => {
      const largeArray = Array.from({ length: 1000 }, (_, i) => i * 2)
      return searcher.binarySearch([...largeArray], 0) === 0 &&
             searcher.binarySearch([...largeArray], 100) === 50 &&
             searcher.binarySearch([...largeArray], 1998) === 999 &&
             searcher.binarySearch([...largeArray], 1) === -1
    })()
  )

  console.log(
    "7. this上下文二分搜索测试:",
    (() => {
      const binaryObj = {
        multiplier: 2,
        processBinarySearch: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.binarySearch(arr, target)
          return result * this.multiplier
        }
      }
      return binaryObj.processBinarySearch([1, 3, 5, 7, 9], 5) === 4 // 2 * 2 = 4
    })()
  )

  console.log(
    "8. 复杂对象this绑定二分搜索测试:",
    (() => {
      const binaryConfigObj = {
        config: { offset: 10 },
        processWithConfig: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.binarySearch(arr, target)
          return result + this.config.offset
        }
      }
      return binaryConfigObj.processWithConfig([1, 3, 5, 7, 9], 3) === 11 // 1 + 10 = 11
    })()
  )
}

testBinarySearchDetailed()
